/**
 * Created by gm on 2016/8/4.
 */
var express=require('express');
var url=require('url');
var router=express.Router();

var monk=require('monk');
var db=monk('localhost:27017/usercollection');
var collection=db.get('messagecollection');

router.get('/',function(req,res){
    collection.find({},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.post('/',function(req,res){
    function resResult(data){
        var result=[];
        for(var i=0;i<data.length;i++){
            var isExist=false;
            for(var j=0;j<result.length;j++){
                if(data[i].appId==result[j].appId){
                    isExist=true;
                    if(data[i].status==0){
                        result[j].num_success=data[i].count;
                    }else{
                        result[j].num_fail=data[i].count;
                    }
                }
            }
            if(isExist==false){
                var item={appId:data[i].appId,num_success:0,num_fail:0};
                if(data[i].status==0){
                    item.num_success=data[i].count;
                }else{
                    item.num_fail=data[i].count;
                }
                result.push(item);
            }
        }
        var obj={data:result};
        return obj;
    }
    if(req.body!=null){
        var pageSize=parseInt(req.body.pageSize);
        var skip=parseInt((req.body.p_current-1)*pageSize);
        if(req.body.name=='smsSend'){
            collection.count({},function(err,data){
                if(err) throw err;
                collection.find({},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=='searchMessage'){
            var con=new RegExp(req.body.content);
            collection.count({"$or":[{appId:con},{content:con},{mobile:con}]},function(err,data){
                if(err) throw err;
                collection.find({"$or":[{appId:con},{content:con},{mobile:con}]},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=='statistic'){
            var isAppIdEmpty=req.body.appId==undefined||req.body.appId=='';
            var isStartEmpty=req.body.startTime==undefined||req.body.startTime=='';
            var isEndEmpty=req.body.endTime==undefined||req.body.endTime=='';
            if(isAppIdEmpty&&isStartEmpty&&isEndEmpty){
                collection.aggregate([{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(!isAppIdEmpty&&isStartEmpty&&isEndEmpty){
                collection.aggregate([{$match:{appId:req.body.appId}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(isAppIdEmpty&&!isStartEmpty&&isEndEmpty){
                collection.aggregate([{$match:{sendTime:{$gte:req.body.startTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(isAppIdEmpty&&isStartEmpty&&!isEndEmpty){
                collection.aggregate([{$match:{sendTime:{$lte:req.body.endTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(!isAppIdEmpty&&!isStartEmpty&&isEndEmpty){
                collection.aggregate([{$match:{appId:req.body.appId,sendTime:{$gte:req.body.startTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(!isAppIdEmpty&&isStartEmpty&&!isEndEmpty){
                collection.aggregate([{$match:{appId:req.body.appId,sendTime:{$lte:req.body.endTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(isAppIdEmpty&&!isStartEmpty&&!isEndEmpty){
                collection.aggregate([{$match:{sendTime:{$gte:req.body.startTime,$lte:req.body.endTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
            if(!isAppIdEmpty&&!isStartEmpty&&!isEndEmpty){
                collection.aggregate([{$match:{appId:req.body.appId,sendTime:{$gte:req.body.startTime,$lte:req.body.endTime}}},{$group:{"_id":{"appId":"$appId","status":"$status","sendTime":"$sendTime"}}},
                    {$group:{"_id":{"appId":"$_id.appId","status":"$_id.status"},count:{$sum:1}}},
                    {$project:{"_id":0,"appId":"$_id.appId","status":"$_id.status","count":1}},
                    {$sort:{appId:1}}
                ],function(err,data){
                    if(err) throw err;
                    res.json(resResult(data));
                });
            }
        }
    }else{
        res.send('{"status":"failed"}');
    }
});
router.get('/:id',function(req,res){
    collection.findOne({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.get('/:appId',function(req,res){
    collection.findOne({appId:req.params.appId},function(err,data){
        if(err) throw err;
        res.json(data);
    })
});
router.put('/:id',function(req,res){
    collection.update({
            _id:req.body._id
        },
        {$set:
        {secretKey:req.body.secretKey}
        },function(err,data){
            if(err) throw err;
            res.json(data);
        });
});

router.delete('/:id',function(req,res){
    collection.remove({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});
module.exports=router;